<template>
    <el-drawer
    :title="!dataForm.id ? '新增' : '修改'"
    direction="rtl"
    ref="drawer"
    size="40%"
    :visible.sync="visible">
    <div class="demo-drawer__header">
      <el-button size="mini" @click="visible = false">取 消</el-button>
      <el-button type="success" size="mini" @click="dataFormSubmit()">确定</el-button>
    </div>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="角色名称"></el-input>
      </el-form-item>
      <el-form-item label="备注信息" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注信息"></el-input>
      </el-form-item>
      <el-form-item label="所在部门" prop="deptId">
        <el-tree-select
          style="width:100%"
          :data="treeData"
          v-model="dataForm.deptId"
          node-key="id"
          placeholder="请选择部门"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="dataForm.status" placeholder="请选择状态" style="width:100%">
            <el-option
              v-for="item in statuses"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
      </el-form-item>
    </el-form>
  </el-drawer>
</template>

<script>
  import ElTreeSelect from "@/components/treeSelect/index.vue"
  export default {
    data () {
      return {
        visible: false,
        treeData: [],
        dataForm: {
          id: 0,
          tenantId: '',
          name: '',
          remark: '',
          deptId: '',
          status: ''
        },
        statuses:[
          {label:'无效', value:0},
          {label:'有效', value:1}
        ],
        dataRule: {
          name: [
            { required: true, message: '角色名称不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注信息不能为空', trigger: 'blur' }
          ],
          deptId: [
            { required: true, message: '部门不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    components: {
      ElTreeSelect
    },
    mounted () {
      this.initOrgTree()
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/tenant/tenantRoleInfo/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.tenantId = data.tenantRoleInfo.tenantId
                this.dataForm.name = data.tenantRoleInfo.name
                this.dataForm.remark = data.tenantRoleInfo.remark
                this.dataForm.deptId = data.tenantRoleInfo.deptId
                this.dataForm.status = data.tenantRoleInfo.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/tenant/tenantRoleInfo/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'tenantId': this.dataForm.tenantId,
                'name': this.dataForm.name,
                'remark': this.dataForm.remark,
                'deptId': this.dataForm.deptId,
                'status': this.dataForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      initOrgTree() {
        this.$http({
          url: this.$http.adornUrl('/tenant/tenantDeptInfo/tree'),
          method: 'get',
          params: this.$http.adornParams({})
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.treeData = [data.deptInfo]
          } else {
            this.treeData = []
          }
        })
      }
    }
  }
</script>

<style lang="scss">
    .demo-drawer {
        &__content {
            display: flex;
            flex-direction: column;
            height: 100%;
            form {
                flex: 1;
            }
        }
        &__footer {
            display: flex;
            button {
                flex: 1;
            }
        }
        &__header {
            text-align:right;
            margin-bottom:20px;
            width:100%;
        }
    }

    .el-drawer__body {
        padding: 20px;
    }
    .el-input__inner{
      height: 36px;
    }
</style>
